package com.banciyuan.bcywebview.utils.download;

import android.os.Environment;
import android.util.Log;

import com.banciyuan.bcywebview.utils.universalimageloader.cache.disc.naming.FileNameGenerator;
import com.banciyuan.bcywebview.utils.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.IoUtils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by squall on 2015/10/22.
 */
public class DownLoadUtils {

    private static final String TEMP_IMAGE_POSTFIX = ".tmp";

    private static final int bufferSize=3*1024;

    /** Returns file object (not null) for incoming image URI. File object can reference to non-existing file. */
    public static File getFile(String imageUri) {
        FileNameGenerator fileNameGenerator= new Md5FileNameGenerator();
        String fileName = fileNameGenerator.generate(imageUri);
        String rootpath=getAlbumStorageDir("半次元").getAbsolutePath();
        if(imageUri.contains("png")){
            return new File(rootpath, fileName+".png");
        }else if(imageUri.contains("jpg")){
            return new File(rootpath, fileName+".jpg");
        }else if(imageUri.contains("gif")){
            return new File(rootpath, fileName+".gif");
        }else{
            return new File(rootpath, fileName);
        }

    }

    public static boolean save(String imageUri, InputStream imageStream, IoUtils.CopyListener listener) throws IOException {
        File imageFile = getFile(imageUri);
        File tmpFile = new File(imageFile.getAbsolutePath() + TEMP_IMAGE_POSTFIX);
        boolean loaded = false;
        try {
            OutputStream os = new BufferedOutputStream(new FileOutputStream(tmpFile), bufferSize);
            try {
                loaded = IoUtils.copyStream(imageStream, os, listener, bufferSize);
            } finally {
                IoUtils.closeSilently(os);
            }
        } finally {
            IoUtils.closeSilently(imageStream);
            if (loaded && !tmpFile.renameTo(imageFile)) {
                loaded = false;
            }
            if (!loaded) {
                tmpFile.delete();
            }
        }
        return loaded;
    }

    public static boolean saveReal(String imageUri, InputStream imageStream, IoUtils.CopyListener listener) throws IOException {
        File imageFile = getFile(imageUri);

        File tmpFile = new File(imageFile.getAbsolutePath() + TEMP_IMAGE_POSTFIX);
        boolean loaded = false;
        try {
            OutputStream os = new BufferedOutputStream(new FileOutputStream(tmpFile), bufferSize);
            try {
                loaded = IoUtils.copyStreamReal(imageStream, os, listener, bufferSize);
            } finally {
                IoUtils.closeSilently(os);
            }
        } finally {
            IoUtils.closeSilently(imageStream);
            if (loaded && !tmpFile.renameTo(imageFile)) {
                loaded = false;
            }
            if (!loaded) {
                tmpFile.delete();
            }
        }
        return loaded;
    }

    public static File getAlbumStorageDir(String albumName) {
        // Get the directory for the user's public pictures directory.
        File file = new File(Environment.getExternalStoragePublicDirectory(
                "DCIM"), albumName);
        if (!file.mkdirs()) {
            Log.e("Directory", "Directory not created");
        }
        return file;
    }
}
